const HTTPserver = {
  getUsefWebs: function (usefWebs) {
    fetch('http://localhost:8080/common/getUsefWebs', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          const url = '../../src/assets/usefWebs/';
          json.data.forEach(item => {
            item.img = url + item.img;
          });
          usefWebs.value = json.data;
        } else {
          alert(json.msg);
        }
      });
  },
  addUsefWebs: function (newUsefWebs, callback) {
    const formData = new FormData();
    formData.append('file', newUsefWebs.value.selectedFile);
    formData.append('title', newUsefWebs.value.title);
    formData.append('description', newUsefWebs.value.description);
    formData.append('url', newUsefWebs.value.url);
    fetch('http://localhost:8080/common/addUsefWebs', {
      mode: 'cors', //跨域请求
      method: 'POST',
      body: formData,
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          callback();
        } else {
          alert(json.msg);
        }
      });
  },
  delUsefWebs: function (UsefWebs, callback) {
    fetch('http://localhost:8080/common/delUsefWebs', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(UsefWebs),
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          callback();
        } else {
          alert(json.msg);
        }
      });
  },
  getWebLog: function (webLog) {
    fetch('http://localhost:8080/common/getWebLog', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          webLog.value = json.data;
        } else {
          alert(json.msg);
        }
      });
  },
  addWebLog: function (newWebLog, callback) {
    let obj = {
      title: newWebLog.value.title,
      description: newWebLog.value.description,
      date: newWebLog.value.date,
    };
    fetch('http://localhost:8080/common/addWebLog', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(obj),
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          callback();
        } else {
          alert(json.msg);
        }
      });
  },

  delWebLog: function (WebLog, callback) {
    fetch('http://localhost:8080/common/delWebLog', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(WebLog),
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          callback();
        } else {
          alert(json.msg);
        }
      });
  },

  getRecoIdea: function (recoIdea) {
    fetch('http://localhost:8080/common/getRecoIdea', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          recoIdea.value = json.data;
        } else {
          alert(json.msg);
        }
      });
  },
  getRecoIdeaBySort: function (recoIdea, sort) {
    if (sort > 20000) {
      return;
    }
    let obj = {
      sort: sort,
    };
    fetch('http://localhost:8080/common/getRecoIdeaBySort', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(obj),
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          recoIdea.value = json.data;
        } else {
          alert(json.msg);
        }
      });
  },
  addRecoIdea: function (newRecoIdea, callback) {
    let obj = {
      sessionRequst: {
        session: localStorage.getItem('session'),
      },
      recoIdeaEntity: {
        sort: newRecoIdea.value.sort,
        title: newRecoIdea.value.title,
        text: newRecoIdea.value.text,
        date: newRecoIdea.value.date,
      },
    };
    fetch('http://localhost:8080/common/addRecoIdea', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(obj),
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          callback();
        } else {
          alert(json.msg);
        }
      });
  },

  delRecoIdea: function (recoIdea, callback) {
    fetch('http://localhost:8080/common/delRecoIdea', {
      mode: 'cors', //跨域请求
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(recoIdea),
      method: 'POST',
    })
      .then(response => response.json())
      .then(json => {
        if (json.code == '200') {
          callback();
        } else {
          alert(json.msg);
        }
      });
  },
};

export default HTTPserver;
